//package work.mediway.mdm.biz.service.biz.impl;
//
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.mediway.hos.app.base.seure.util.SecurityUtils;
//
//import org.springframework.stereotype.Service;
//
//import java.sql.Connection;
//import java.util.List;
//
//import javax.annotation.Resource;
//
//import cn.hutool.core.collection.CollUtil;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.lang.Assert;
//import cn.hutool.core.util.ObjectUtil;
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.db.DbUtil;
//import cn.hutool.db.handler.BeanListHandler;
//import cn.hutool.db.sql.SqlExecutor;
//import cn.hutool.json.JSONUtil;
//import lombok.extern.slf4j.Slf4j;
//import work.mediway.mdm.biz.bo.DataQualityConnectionBO;
//import work.mediway.mdm.core.enums.DataQualityDealFlagEnum;
//import work.mediway.mdm.core.enums.DataQualityExceptionDealTypeEnum;
//import work.mediway.mdm.core.enums.DataQualityExceptionTypeEnum;
//import work.mediway.mdm.core.exception.DataException;
//import work.mediway.mdm.core.transport.ErrorMsgVO;
//import work.mediway.mdm.biz.dto.biz.DataQualityDataExceptionDealDTO;
//import work.mediway.mdm.biz.dto.biz.DataQualityVerifyDTO;
//import work.mediway.mdm.biz.entity.biz.SsDataQualityConfig;
//import work.mediway.mdm.biz.entity.biz.SsDataQualityLog;
//import work.mediway.mdm.biz.entity.biz.SsDataQualityLogItem;
//import work.mediway.mdm.biz.mapper.biz.SsDataQualityLogItemMapper;
//import work.mediway.mdm.biz.service.biz.SsDataQualityConfigService;
//import work.mediway.mdm.biz.service.biz.SsDataQualityLogItemService;
//import work.mediway.mdm.biz.service.biz.SsDataQualityLogService;
//import work.mediway.mdm.biz.dataQuality.DataQualityDataExceptionDealStrategyFactory;
//
///**
// * @author zhongyj <1126834403@qq.com><br/>
// * @date 2022/8/5
// */
// TODO: 2022/10/6
//@Slf4j
//@Service
//public class SsDataQualityLogItemServiceImpl extends ServiceImpl<SsDataQualityLogItemMapper, SsDataQualityLogItem> implements SsDataQualityLogItemService {
//
//    @Resource
//    private SsDataQualityLogService dataQualityLogService;
//    @Resource
//    private SsDataQualityConfigService dataQualityConfigService;
//
//    @Override
//    public List<SsDataQualityLogItem> selectDataQualityLogItem(SsDataQualityLogItem dataQualityLogItem) {
//        List<SsDataQualityLogItem> dataQualityLogItems = this.baseMapper.selectDataQualityLogItem(dataQualityLogItem);
//        SsDataQualityConfig dataQualityConfig = null;
//        if (ObjectUtil.isNotEmpty(dataQualityLogItem.getLogId()) && CollUtil.isNotEmpty(dataQualityLogItems)) {
//            SsDataQualityLog dataQualityLog = this.dataQualityLogService.getById(dataQualityLogItems.get(0).getLogId());
//
//            SsDataQualityConfig config = new SsDataQualityConfig();
//            config.setId(Integer.valueOf(dataQualityLog.getDataQualityConfigId().toString()));
//            List<SsDataQualityConfig> dataQualityConfigs = this.dataQualityConfigService.selectDataQualityConfig(config);
//            if (CollUtil.isNotEmpty(dataQualityConfigs)) {
//                dataQualityConfig = dataQualityConfigs.get(0);
//            }
//        }
//        if (ObjectUtil.isNotEmpty(dataQualityConfig)) {
//            for (SsDataQualityLogItem qualityLogItem : dataQualityLogItems) {
//                assert dataQualityConfig != null;
//                qualityLogItem.setSourceAccessSystemId(dataQualityConfig.getSourceAccessSystemId());
//                qualityLogItem.setSourceAccessSystem(dataQualityConfig.getSourceAccessSystem());
//                qualityLogItem.setTargetAccessSystemId(dataQualityConfig.getTargetAccessSystemId());
//                qualityLogItem.setTargetAccessSystem(dataQualityConfig.getTargetAccessSystem());
//                if (StrUtil.isNotBlank(dataQualityConfig.getSourceSchemaName())) {
//                    qualityLogItem.setSourceTableCode(StrUtil.join(StrUtil.DOT, dataQualityConfig.getSourceSchemaName(), dataQualityConfig.getSourceTableCode()));
//                } else {
//                    qualityLogItem.setSourceTableCode(dataQualityConfig.getSourceTableCode());
//                }
//                if (StrUtil.isNotBlank(dataQualityConfig.getTargetSchemaName())) {
//                    qualityLogItem.setTargetTableCode(StrUtil.join(StrUtil.DOT, dataQualityConfig.getTargetSchemaName(), dataQualityConfig.getTargetTableCode()));
//                } else {
//                    qualityLogItem.setTargetTableCode(dataQualityConfig.getTargetTableCode());
//                }
//            }
//        }
//        return dataQualityLogItems;
//    }
//
//
//    @Override
//    public ErrorMsgVO dataQualityDataExceptionDeal(List<DataQualityDataExceptionDealDTO> dataExceptionDeals) {
//        ErrorMsgVO msgVO = new ErrorMsgVO();
//        for (DataQualityDataExceptionDealDTO exceptionDeal : dataExceptionDeals) {
//            try {
//                SsDataQualityLogItem dataQualityLogItem = this.getById(exceptionDeal.getLogItemId());
//                SsDataQualityLog dataQualityLog = this.dataQualityLogService.getById(dataQualityLogItem.getLogId());
//                SsDataQualityConfig dataQualityConfig = this.dataQualityConfigService.getById(dataQualityLog.getDataQualityConfigId());
//                exceptionDeal.setDataQualityConfig(dataQualityConfig);
//                switch (exceptionDeal.getDataQualityExceptionDealTypeEnum()) {
//                    case IGNORE:
//                        DataQualityDataExceptionDealStrategyFactory.getStrategy(DataQualityExceptionDealTypeEnum.IGNORE).dealDataQualityExceptionValue(exceptionDeal);
//                        break;
//                    case IMPORT:
//                        DataQualityDataExceptionDealStrategyFactory.getStrategy(DataQualityExceptionDealTypeEnum.IMPORT).dealDataQualityExceptionValue(exceptionDeal);
//                        break;
//                    case DELETE:
//                        DataQualityDataExceptionDealStrategyFactory.getStrategy(DataQualityExceptionDealTypeEnum.DELETE).dealDataQualityExceptionValue(exceptionDeal);
//                        break;
//                    default:
//                        throw new DataException(exceptionDeal.getDataQualityExceptionDealTypeEnum().name() + "暂未支持");
//                }
//                SsDataQualityLogItem ssDataQualityLogItem = new SsDataQualityLogItem();
//                ssDataQualityLogItem.setId(Long.valueOf(exceptionDeal.getLogItemId()));
//                ssDataQualityLogItem.setDealFlag(DataQualityDealFlagEnum.DEAL.getType());
//                ssDataQualityLogItem.setDealType(exceptionDeal.getDataQualityExceptionDealTypeEnum().getType());
//                ssDataQualityLogItem.setDealDate(DateUtil.now());
//                ssDataQualityLogItem.setDealUser(SecurityUtils.getLoginName());
//                this.updateById(ssDataQualityLogItem);
//            } catch (Exception e) {
//                e.printStackTrace();
//                msgVO.setErrorMsg(JSONUtil.toJsonStr(exceptionDeal), e.getMessage());
//            }
//        }
//        msgVO.setCount(dataExceptionDeals.size());
//        return msgVO;
//    }
//
//    @Override
//    public ErrorMsgVO dataQualityDataExceptionOperaDeal(List<String> logItemIds) {
//        ErrorMsgVO msgVO = new ErrorMsgVO();
//        DataQualityConnectionBO dataQualityConnectionBO = new DataQualityConnectionBO();
//        Connection connection = null;
//        for (String logItemId : logItemIds) {
//            try {
//                // 查询日志详情数据
//                SsDataQualityLogItem qualityLogItem = this.getById(Long.valueOf(logItemId));
//                // 日志记录
//                SsDataQualityLog dataQualityLog = this.dataQualityLogService.getById(qualityLogItem.getLogId());
//                // 根据查询的配置id获取源系统和目标系统数据库连接信息
//                DataQualityVerifyDTO dataQualityVerifyDTO = new DataQualityVerifyDTO();
//                dataQualityVerifyDTO.setConfigId(String.valueOf(dataQualityLog.getDataQualityConfigId()));
//                dataQualityConfigService.verifyAndGetConnection(dataQualityVerifyDTO, dataQualityConnectionBO);
//                // 手动比对
//                String sql = null;
//                if (qualityLogItem.getDataExceptionType().equals(DataQualityExceptionTypeEnum.SOURCE_MISS.getType())) {
//                    connection = dataQualityConnectionBO.getSourceConnection();
//                    sql = dataQualityConnectionBO.buildSourceSelectSql(CollUtil.newArrayList(qualityLogItem.getLogUnique()));
//                } else if (qualityLogItem.getDataExceptionType().equals(DataQualityExceptionTypeEnum.TARGET_MISS.getType())) {
//                    connection = dataQualityConnectionBO.getTargetConnection();
//                    String logUnique = qualityLogItem.getLogUnique();
//                    if (dataQualityConnectionBO.isMdm()) {
//                        logUnique = "00001_" + logUnique;
//                    }
//                    sql = dataQualityConnectionBO.buildTargetSelectSql(CollUtil.newArrayList(logUnique));
//                } else {
//                    msgVO.setErrorMsg(logItemId, "非数量差异");
//                }
//                if (connection != null && sql != null) {
//                    Assert.notNull(connection);
//                    log.info("手动比对查询数据SQL为: {}", sql);
//                    List<String> result = SqlExecutor.query(connection, sql, new BeanListHandler<>(String.class));
//
//                    SsDataQualityLogItem dataQualityLogItem = new SsDataQualityLogItem();
//                    if (CollUtil.isNotEmpty(result)) {
//                        dataQualityLogItem.setId(Long.valueOf(logItemId));
//                        dataQualityLogItem.setOperaThanFlag(1);
//                    } else {
//                        dataQualityLogItem.setId(Long.valueOf(logItemId));
//                        dataQualityLogItem.setOperaThanFlag(0);
//                    }
//                    this.updateById(dataQualityLogItem);
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                msgVO.setErrorMsg(logItemId, e.getMessage());
//            } finally {
//                if (connection != null) {
//                    DbUtil.close(connection);
//                }
//            }
//        }
//        msgVO.setCount(logItemIds.size());
//        return msgVO;
//    }
//}
